﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReferenceScheduler.ConferenceModule;

namespace ReferenceScheduler.Strategy
{
    class SessionScheduler
    {
        List<Talk> givenTalks;
        List<Talk> chosenTalks;
        List<Talk> returnedTalks;
        int[,] maxValue;
        bool[,]used;
        int sessionLength;
              
        public SessionScheduler(int sessionLength, List<Talk> givenTalks)
        {
            this.givenTalks = givenTalks;
            this.chosenTalks = new List<Talk>();
            this.returnedTalks = new List<Talk>();
            this.sessionLength = sessionLength;
            InitValueArray();
        }       

        public void InitValueArray()   
        {
            maxValue = new int[sessionLength + 1,givenTalks.Count ];
            used = new bool[sessionLength + 1,givenTalks.Count ];

            for(int va = 0; va <= sessionLength; va++)
            {
                maxValue[va,0]= 0; 
            }

            for(int o = 0; o < givenTalks.Count; o++ )
            {
                maxValue[0,o] = 0;
            }
        }

        public void SelectTalk() 
        {
            CalculateValueArray();
            SetChosenTalks();
            SetReturnedTalks();
        }

        public void CalculateValueArray( ) {
            for (int s = 1; s <= sessionLength; s++ )
            {
                for (int o = 1; o < givenTalks.Count ; o++)
                {
                    if (givenTalks[o].GetInterval() <= s)
                    {
                        maxValue[s, o] = Math.Max(maxValue[s, o - 1], 
                                        maxValue[s - givenTalks[o].GetInterval(), o - 1] + givenTalks[o].GetInterval());
                        
                        if (maxValue[s, o] == maxValue[s, o - 1])
                        {
                            used[s, o] = false;
                        }
                        else used[s, o] = true;
                    }
                    else { 
                        maxValue[s, o] = maxValue[s,o-1];
                        used[s,o] = false;
                    }
                }
            }
        }

        public void SetChosenTalks()
        {    
            int left  = maxValue[sessionLength,givenTalks.Count - 1];
            for (int i = givenTalks.Count - 1; i > 0; i-- )
            {
                if (used[left, i] == true) 
                {
                    chosenTalks.Add(givenTalks[i]);
                    left = left - givenTalks[i].GetInterval();
                }
            }
        }

        public void SetReturnedTalks()
        { 
            foreach(Talk t in chosenTalks)
            {
                givenTalks.Remove(t);
            }
            returnedTalks = givenTalks;
        }

        public List<Talk> GetReturnedTalks()
        {
            return returnedTalks;
        }

        public List<Talk> GetChosenTalks()
        {
            return chosenTalks;
        }

        public string ToString()
        {
            string myString = "";
            for (int i = 0; i < chosenTalks.Count; i++ )
            {
                myString += givenTalks[i].GetTitle() + givenTalks[i].GetInterval() + "\r\n";
            }
            return myString;
        }
                
    }
}

